﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Emgu.CV;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Cuda;

namespace Catastrophe.Emgucv.ViewMode
{
    /// <summary>
    /// 相机
    /// </summary>
    public partial class ucEmgucvCameraModel : ObservableObject
    {
        /// <summary>
        /// 相机
        /// </summary>
        public ICommand OpenCapture { get; set; }
        /// <summary>
        /// 读取相机
        /// </summary>
        public ICommand CaptureRead { get; set; }
        /// <summary>
        /// 人脸识别
        /// </summary>
        public ICommand CascadeRead { get; set; }
        public Emgu.CV.VideoCapture videoCapture { get; set; }
        /// <summary>
        /// 
        /// </summary>
        CascadeClassifier fase { get; set; }
        /// <summary>
        /// 显示的图片
        /// </summary>
        [ObservableProperty]
        private BitmapSource imageBrush1;
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        private BitmapSource imageBrush2;
        public ucEmgucvCameraModel()
        {
            OpenCapture = new RelayCommand(OpenCaptureMethod);
            CaptureRead = new RelayCommand(CaptureReadMethod);
            CascadeRead = new RelayCommand(CascadeReadMethod);
            fase = new CascadeClassifier("xmlModel\\haarcascade_frontalface_alt2.xml");
        }
        /// <summary>
        /// 人脸识别
        /// </summary>
        private void CascadeReadMethod()
        {
            var camera = BitmapSourceExtension.ToMat(ImageBrush1);
            var mat = new Mat();
            //灰度
            CvInvoke.CvtColor(camera, mat, ColorConversion.Bgr2Gray);
            //亮度增强
            CvInvoke.EqualizeHist(mat, mat);

            var facesDetected = fase.DetectMultiScale(mat);
            var dstmat = new Mat();
            camera.CopyTo(dstmat);
            if (facesDetected.Length>0)
            {
                for (Int32 i = 0; i < facesDetected.Length; i++)
                {
                    CvInvoke.Rectangle(dstmat, facesDetected[i], new MCvScalar(120,120,120));
                }
            }
            ToImage2(dstmat);
        }

        private void CaptureReadMethod()
        {
            if (videoCapture == null)
            {
                OpenCaptureMethod();
            }
            var mat = new Mat();
            videoCapture.Read(mat);
            ToImage1(mat);
        }

        private void OpenCaptureMethod()
        {
            videoCapture = new Emgu.CV.VideoCapture(0);
        }
        private void ToImage1(Mat mat)
        {
            ImageBrush1 = Emgu.CV.BitmapSourceExtension.ToBitmapSource(mat);
        }
        private void ToImage2(Mat mat)
        {
            ImageBrush2 = Emgu.CV.BitmapSourceExtension.ToBitmapSource(mat);
        }
    }
}
